package com.xinguan.controller;

import com.github.pagehelper.PageInfo;
import com.xinguan.common.Result;
import com.xinguan.entity.*;
import com.xinguan.pojo.dto.AdminDto;
import com.xinguan.entity.Advertisement;
import com.xinguan.pojo.dto.ChangePasswordDto;
import com.xinguan.pojo.dto.CommodityDto;
import com.xinguan.pojo.dto.LoginDto;
import com.xinguan.service.*;
import com.xinguan.utils.MD5;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private TypesService typesService;
    @Autowired
    private InformationService informationService;
    @Autowired
    private AdvertisementService advertisementService;

    //登录
    @ApiOperation("登录")
    @PostMapping("/admins/login")
    public Result login(@RequestBody LoginDto loginDto) {
        Map<String, Object> data = adminService.login(loginDto);
        return (Boolean) data.get("isSuccess") ? Result.success(data)
                : Result.fail(20002, "用户名或密码错误");
    }

    //注册
    @ApiOperation("注册")
    @PostMapping("/admins/register")
    public Result registerUser(@RequestBody AdminDto adminDto) {
        if (adminService.isUsernameTaken(adminDto.getAdminaccount())) {
            return Result.fail(2000, "用户名已经建立");
        }
        Admin user = new Admin();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        user.setId(uuid);
        user.setAdminaccount(adminDto.getAdminaccount());
        String password = adminDto.getConfirmPassword();
        String passwordMD5 = MD5.encrypt(password);
        user.setAdminpwd(passwordMD5);
        user.setName(adminDto.getName());
        if (adminDto.getAdminpwd().equals(adminDto.getConfirmPassword())) {
            adminService.registerUser(user);
            return Result.success("注册成功！");
        } else {
            return Result.fail("请重新确认你输入的密码");
        }
    }
    @ApiOperation("修改密码")
    @PutMapping("/users/user")
    public Object updatePassword(@RequestBody ChangePasswordDto changePasswordDto) {
        usersService.updatePassword(changePasswordDto);
        return Result.success(200, "修改密码成功");

    }
    @ApiOperation("删除用户")
    @DeleteMapping("/users/user/{id}")
    public Object deleteAdmin(@PathVariable("id") String id) {
        usersService.deleteAdmin(id);
        return Result.success(200, "删除用户成功");

    }

    //获取单个用户
    @ApiOperation("获取单个用户")
    @GetMapping("/users/user/{id}")
    public Object getAdmin(@PathVariable("id") String id) {
        Map<String, Object> data = usersService.getUsers(id);
        return Result.success("返回信息成功", data);
    }

    //分页获取用户列表
    @ApiOperation("分页获取用户列表")
    @GetMapping("/users/all-users/{current}/{pageSize}")
    public Object getEmployeePage(@PathVariable("current") Integer current, @PathVariable("pageSize") Integer pageSize) {
        PageInfo<Users> page = usersService.getEmployeePage(current, pageSize);
        return Result.success("获取用户列表成功", page);
    }

    //新增商品
    @ApiOperation("新增商品")
    @PostMapping("/commodities/commodity")
    public Object increaseCommodity(@RequestBody CommodityDto commodityDto) {
        commodityService.increaseCommodity(commodityDto);
        return Result.success();
    }

    //分页获取商品
    @ApiOperation("分页获取商品")
    @GetMapping("/commodities/all-commodities")
    public Object getCommoditiesPage(@RequestParam(value = "category_id", required = false) String id, @RequestParam(value = "commodityName", required = false) String commodityName, @RequestParam(value = "inventoryStatus", required = false) Integer inventoryStatus,
                                     @RequestParam(value = "popularity", required = false) Integer popularity, @RequestParam(required = true) Integer count, @RequestParam(required = true) Integer pageSize) {
        PageInfo<Commodity> page = commodityService.getCommodities(id, commodityName, inventoryStatus, popularity, count, pageSize);

        return Result.success("获取用户列表成功", page);
    }

    //新增商品分类
    @ApiOperation("新增商品分类")
    @PostMapping("/categories/category")
    public Object increaseCategory(@RequestBody Types types) {
        boolean a = typesService.increaseCategory(types);
        if (a) {
            return Result.success("新增商品成功");
        } else {
            return Result.fail("该类已经存在");
        }

    }

    //分页获取商品分类
    @ApiOperation("分页获取商品分类")
    @GetMapping("/categories/all-categories/{count}/{pageSize}")
    public Object getTypesPage(@PathVariable("count") Integer count, @PathVariable("pageSize") Integer pageSize) {
        PageInfo<Types> page = typesService.getTypesPage(count, pageSize);
        return Result.success("获取分类列表成功", page);
    }

    //修改商品信息
    @ApiOperation("修改商品信息")
    @PutMapping("/commodities/commodity")
    public Object updateCommodity(@RequestBody Commodity commodity) {
        commodityService.updateCommodity(commodity);
        return Result.success();
    }

    //删除商品分类
    @ApiOperation("删除商品分类")
    @DeleteMapping("/categories/category/{id}/{curToal}")
    public Object deleteTypes(@PathVariable("id") String id, @PathVariable("curToal") Integer curToal) {
        Boolean isNull = typesService.deleteTypes(id, curToal);
        if (isNull) {
            return Result.fail("有商品存在不可以删除！");
        }
        return Result.success("删除成功！");
    }

    //新增广告
    @ApiOperation("新增广告")
    @PostMapping("/ads/ad")
    public Object increaseAdvertisement(@RequestBody Advertisement advertisement) {
        advertisementService.increaseAdvertisement(advertisement);
        return Result.success();

    }

    //获取所有广告
    @ApiOperation("获取所有广告")
    @GetMapping("/ads/all-ads")
    public Object getAllAdvertisement() {
        List<Advertisement> all = advertisementService.getAllAdvertisement();
        return Result.success(all);
    }

    @ApiOperation("修改广告图片")
    @PutMapping("/ads/ad")
    public Object updatePhoto(@RequestBody Advertisement advertisement) {

        advertisementService.updatePhoto(advertisement);
        return Result.success();

    }
    @ApiOperation("删除广告")
    @DeleteMapping("/ads/ad/delete")
    public Object deleteAdvertisement(@RequestBody Advertisement advertisement) {
        advertisementService.deleteAdvertisement(advertisement);
        return Result.success();
    }

    //订单信息管理
    @ApiOperation("订单信息管理")
    @GetMapping("/order/information")
    public Object getAllInformation(@RequestParam String orderStatus) {
        PageInfo<AllInformation> page = informationService.getAllInformation(orderStatus);
        return Result.success(page);
    }

    //修改状态
    @ApiOperation("修改订单状态")
    @PutMapping("/order/information")
    public Object updateInformation(@RequestParam String operate, @RequestParam String orderId) {

        informationService.updateInformation(operate, orderId);
        return Result.success();
    }

    //根据条件查询
    @ApiOperation("根据条件查询")
    @GetMapping("/order/someinformation")
    public Object getSomeInformation(@RequestParam(value = "orderId", required = false) String orderId, @RequestParam(value = "phone", required = false) Integer phone,
                                     @RequestParam(value = "beginTime", required = false) LocalDateTime beginTime, @RequestParam(value = "endTime", required = false) LocalDateTime endTime) {
        PageInfo<AllInformation> page = informationService.getSomeInformation(orderId, phone, beginTime, endTime);
        return Result.success(page);
    }


}
